package com.example.careycai.notepad;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends AppCompatActivity {
    private Button btn1;
    private EditText et1,et2,et3;
    private CheckBox ct;
    private MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbHelper =new MyDatabaseHelper(this,"Notepad.db",null,1);
        Goto();

    }
    private void Goto() {
        btn1=(Button)findViewById(R.id.button);
        et1=(EditText)findViewById(R.id.editText);
        et2=(EditText)findViewById(R.id.editText2);
        et3=(EditText)findViewById(R.id.editText3);
        ct=(CheckBox)findViewById(R.id.checkBox);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et1.getText().toString().trim().length()!=0&&et2.getText().toString().trim().length()!=0
                        &&et3.getText().toString().trim().length()!=0){
                    if(et2.getText().toString().equals(et3.getText().toString())){
                        if(ct.isChecked()){

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("name",et1.getText().toString());
                values.put("password", et2.getText().toString());
                db.insert("register", null, values);
                values.clear();

                Toast.makeText(Register.this, "恭喜你注册成功！", Toast.LENGTH_LONG).show();
                            Intent intent=new Intent(Register.this,Signin.class);
                            startActivity(intent);
                            finish();


                        }else{
                            AlertDialog.Builder dialog=new AlertDialog.Builder(Register.this);
                            dialog.setTitle("警告");
                            dialog.setIcon(R.mipmap.ic_launcher);
                            dialog.setMessage("请阅读并同意隐藏政策,服务条款");
                            dialog.create();
                            dialog.show();

                        }
                    }else {
                        AlertDialog.Builder dialog=new AlertDialog.Builder(Register.this);
                        dialog.setTitle("警告");
                        dialog.setIcon(R.mipmap.ic_launcher);
                        dialog.setMessage("两次密码不一致");
                        dialog.create();
                        dialog.show();

                    }
                }else{
                    AlertDialog.Builder dialog=new AlertDialog.Builder(Register.this);
                      dialog.setTitle("警告");
                      dialog.setIcon(R.mipmap.ic_launcher);
                      dialog.setMessage("输入用户名或密码不能为空");
                      dialog.create();
                      dialog.show();
                }


            }
        });
    }

}
